package io.renren.modules.order.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lettuce.core.ScriptOutputType;
import io.renren.common.utils.PageUtils;
import io.renren.modules.dishes.entity.MerchantEntity;
import io.renren.modules.merchant.service.MerchantService;
import io.renren.modules.order.dao.MerchantOrderDao;
import io.renren.modules.order.enity.MerchantEntityOrder;
import io.renren.modules.order.enity.MerchantOrderEntityVo;
import io.renren.modules.order.enity.OrderInfo;
import io.renren.modules.order.enity.SkuNum;
import io.renren.modules.order.service.MerchantOrderService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

@Service("MerchantOrderService")
public class MerchantOrderServiceImpl extends ServiceImpl<MerchantOrderDao, MerchantEntityOrder> implements MerchantOrderService {
    @Resource
    MerchantOrderDao merchantOrderDao;
    @Resource
    MerchantService merchantService;
    /**
     * 获取未发货订单
     */
    @Override
    public PageUtils MerchantOrderStatusByPage(Map<String,Object> params,Integer status) {
        System.out.println(params);
        String merchantId = (String)params.get("merchantId");
        String s = (String) params.get("page");
        Integer pageIndex = Integer.valueOf(s);
        String s1 = (String) params.get("limit");
        Integer pageSize = Integer.valueOf(s1);
        IPage<MerchantOrderEntityVo> iPage=new Page<>(pageIndex,pageSize);
        if (status==0){
            status=1;
        }else {
            status=2;
        }
        merchantOrderDao.unShippedByPage(iPage,status,merchantId,null,null);
        return new PageUtils(iPage);
    }

    @Override
    public PageUtils listByPage(String merchantId, Map<String, Object> params) {
        LocalDateTime startDate=null;
        LocalDateTime endDate=null;
        Integer status = null;
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        String date = (String) params.get("date");
        if (date!=null && !date.isEmpty() ){
            LocalDate localDate=LocalDate.parse(date);
            startDate = LocalDateTime.of(localDate, LocalTime.MIN);
            endDate=LocalDateTime.of(localDate, LocalTime.MAX);
        }
        System.out.println(status);
        if (!params.get("status").equals("")){
            status = Integer.valueOf((String) params.get("status"));
        }else {
            status=null;
        }

        IPage<MerchantOrderEntityVo> iPage=new Page<>(Integer.valueOf(page),Integer.valueOf(limit));
        merchantOrderDao.unShippedByPage(iPage,status,merchantId,startDate,endDate);
        return new PageUtils(iPage);
    }

    @Override
    public List<MerchantOrderEntityVo> getMerchantOrder(Integer userId) {
        Long l = Long.valueOf(userId);
        MerchantEntity merchantByUserId = merchantService.getMerchantByUserId(l);
     return merchantOrderDao.getOrderMerchantListByMerId(merchantByUserId.getMerchantId());

    }

    @Override
    public List<MerchantOrderEntityVo> queryListMerchantOrder(String merchantId) {
        return merchantOrderDao.getOrderMerchantListByMerId(merchantId);
    }

    @Override
    public MerchantOrderEntityVo getDetails(String orderCode) throws IOException {
        Long userId = getUserId();
        MerchantEntity merchant = merchantService.getMerchantByUserId(userId);
        MerchantOrderEntityVo details = merchantOrderDao.getDetails(orderCode, merchant.getMerchantId());
        details.setSkuNums(new ArrayList<>());
        String skuNumbers = details.getSkuNumbers();
        List<SkuNum> skuNums = MerchantOrderEntityVo.fromJsonStringToOrderNum(skuNumbers);
        skuNums.forEach(skuNum -> {
            String skuId = skuNum.getSku_id();
            MerchantOrderEntityVo orderInfo=merchantOrderDao.selectInfoBySkuId(skuId);
            if (orderInfo.getOrderInfo()==null||skuNum.getOrderInfo()==null){
                skuNum.setOrderInfo(new OrderInfo());
            }
            skuNum.getOrderInfo().setSpuName(orderInfo.getOrderInfo().getSpuName());
            skuNum.getOrderInfo().setSkuSpecs(orderInfo.getOrderInfo().getSkuSpecs());
            details.getSkuNums().add(skuNum);
        });
        return details;
    }


}
